<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-04 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/ajax/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}

	function excute()
	{
		switch($this->act)
		{
			case 'do_upload':
				$this->doUpload();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				exit();
		}
	}

	private function doUpload()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$url = isset($this->request['url']) ? $this->request['url'] : '';
		
		// for scenic & hostel images, the size of small image is 300*140
		if(stripos($url, 'scenic') > 0 || stripos($url, 'hostel') > 0)
		{
			define('IMAGE_SMALL_2', '300*140');
		}
		
		if(!$id)
		{
			js_alert_output('请指定ID');
		}

		$image_path = '';
		if($_FILES['image']['name'])
		{
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
		}
		
		if($image_path)
		{
			$result = add_travel_item_image($id, $image_path);
			if(!$result)
			{
				delete_image($image_path);
			}
		}
				
		echo "<script type='text/javascript'>";
		echo "window.parent.location.href='".BASE_URL.$url."';";
		echo "</script>";
	}

	private function doDelete()
	{
		$img_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$img = isset($this->request['img']) ? $this->request['img'] : '';
		$url = isset($this->request['url']) ? urldecode($this->request['url']) : '';
		
		if(!$img_id)
		{
			js_alert_output('请选择图片');
		}
		
		if(is_file($img))
		{
			delete_image($img);
		}
		
		delete_travel_item_image($img_id);

		echo "<script type='text/javascript'>";
		echo "window.location.href='".BASE_URL.$url."';";
		echo "</script>";
	}
}